#-*- coding: utf-8 -*-
#! /usr/bin/python

import time
from PAM30 import PAMIE
import win32api
import win32con
import ConfigParser
import winsound


def pai(itemlist, ppass, tid):            
    bindid = '长胡子的小羊'    
    if tid != bindid:
        print 'Bind id not equals config id!'
        return False
    else:
        ie= PAMIE()
        ht = len(itemlist)
        for i in range(ht):
            ie.navigate(itemlist[i])
            time.sleep(3)
            #拍下
            buy = ie.findElement('div', 'id', 'J_juValid')
            a = ie.getElementChildren(buy)
            b = ie.getElementChildren(a[0])        
            eleArry = ie.getElementChildren(buy)
            ie.clickElement(b[0])
            time.sleep(2)        
            #购买
            go = ie.findElement('a', 'id', 'J_Go')
            ie.clickElement(go)
            time.sleep(3)              
            #到支付页面
            pp = ie.findElement('input', 'id', 'payPassword')        
            ie.clickHiddenElement(pp)
            pay = ie.findElement('input', 'id', 'J_authSubmit')
            ie.clickElement(pay)        
            time.sleep(1)
            #输入密码
            psht = len(ppass)
            for j in range(psht):
                win32api.keybd_event(ord(ppass[j]), 0, 0, 0)#按下按键
                #释放按键
                win32api.keybd_event(ord(ppass[j]), 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(1)       
            ie.clickElement(pay)
            time.sleep(3)
            winsound.Beep(370, 200)
            #ie.quit()        

#获取配置文件，转换为list
config = ConfigParser.ConfigParser()
config.read('sys.ini')

itemcount = int(config.get("global", "itemcount"))
#print itemcount
itemlist = range(itemcount)
for i in range(itemcount):
    #print i
    itemlist[i] = config.get("items", "item"+str(i+1))
    #print itemlist[i]

tid = config.get("global", "taobaoid")
ppass = list(config.get("global", "paypass"))

pai(itemlist, ppass, tid)
